<template>

  <el-dialog :title="id ? '关联修改' : '关联添加'" :visible.sync="dialogVisible" :close-on-click-modal="false" width="30%">

    <el-form ref="main-form" :model="form" :rules="rules" label-width="120px" >
      <el-form-item label="选择通道" size="mini">
        <el-select v-model="form.channelId" size="small" clearable :disabled="id != null">
          <el-option
            v-for="item in channelOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="成本费率" size="mini">
        <el-select v-model="form.rateCostId" size="small" clearable>
          <el-option
            v-for="item in rateOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close" size="small">取 消</el-button>
      <el-button type="primary" @click="submit" size="small" :loading="loading">提 交</el-button>
    </div>

  </el-dialog>
</template>

<script>

import ApiMain from '@/api/bank.rateCostRef';
import ChannelApiMain from '@/api/bank.channel';
import RateCostApiMain from '@/api/bank.rateCost';
export default {
  mixins: [
  ],
  props: [
    'banTypeOptions',
  ],
  data () {
    return {

      dialogVisible: false,
      popDeleteVisible: false,
      loading: false,

      form: {
        rateCostId: '',
        channelId : '',
      },

      rules: {
        rateCostId: [
          { required: true, message: '请选择成本费率', trigger: 'blur' },
        ],
        channelId: [
          { required: true, message: '请选择银行通道', trigger: 'blur' },
        ],
      },

      id  : null,
      data: null,

      channelOptions: [],
      rateOptions: [],

    }
  },
  mounted () {
  },

  methods: {

    show (id = null) {

      this.id = id;

      if (this.id) {
        this.fetchData();
      }
      else {
        this.reset();
      }

      // 加载银行通道，成本费率
      this.fetchChannelOption();
      this.fetchRateCostOption();

      this.dialogVisible = true;
    },

    close () {
      this.dialogVisible = false;
      this.$emit('dialog-close');
    },

    reset () {
      this.form = {
        rateCostId: '',
        channelId : '',
      };
    },

    fetchChannelOption () {
      ChannelApiMain.option().then(re => {
       this.channelOptions = re;
      })
    },

    fetchRateCostOption () {
      RateCostApiMain.option().then(re => {
       this.rateOptions = re;
      })
    },

    fetchData () {

      ApiMain.info({id: this.id})
      .then(re => {
        this.data = re; 
      })
      .catch(() => {
        this.data = null;
      })
      .finally(() => {
        this.renderData();
      });
    },

    renderData () {
      this.form.channelId = this.data.channelId;
      this.form.rateCostId = this.data.rateCostId;
    },

    verifyForm () {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit () {

      if (! this.verifyForm()) return false;

      this.loading = true;

      try {
        if (this.id) {
          await this.submitSet();
        }
        else {
          await this.submitAdd();
        }
      } finally {
        this.loading = false;
      }
    },

    async submitDel() {

      this.popDeleteVisible = false;
      this.loading = true;

      await ApiMain.del({id: this.id});

      this.loading = false;
      this.close();
      this.$message({message: '删除成功', type: 'success'});
    },

    async submitSet () {

      this.form.id = this.id;
      await ApiMain.set(this.form);
      this.$message({message: '修改成功', type: 'success'});
      this.close();

    },

    async submitAdd () {

      await ApiMain.add(this.form);
      this.$message({message: '添加成功', type: 'success'});
      this.close();
    },
  }
}
</script>

<style>

</style>
